<?php

namespace app\admin\controller;

use app\admin\annotation\common\Auth;
use app\admin\model\admin\User as UserModel;
use app\admin\validate\admin\User as UserValidate;
use app\common\Constants;
use app\common\facade\Principal;
use app\common\service\VerificationCodeService;
use think\App;

class Profile extends Admin
{

    public function __construct(
        App                               $app,
        protected VerificationCodeService $verificationCodeService,
    )
    {
        parent::__construct($app);
    }

    #[Auth('admin:user:change_password')]
    public function changePassword(): \think\response\Json
    {
        $fields = "oldPassword,newPassword";
        $post = $this->request->only(explode(',', $fields), 'post');

        // 超级管理员不允许修改密码
        if (Principal::isSuperUser()) {
            return $this->error(Constants::E_NOT_CHANGE_SUPER_USER);
        }

        // 查询用户信息
        $userInfo = UserModel::find(Principal::getUserId());

        // 检查旧的密码，需要取消隐藏字段 password
        $userInfo->hidden([]);
        if(!password_verify($post['oldPassword'],$userInfo['password'])){
            return $this->error(['message' => Constants::E_AUTH_PASSWORD_NOT_MATCH]);
        }

        // 模型会自动加密,所以这里只配置明文密码即可
        $data['id'] = $userInfo['id'];
        $data['password'] = $post['newPassword'];

        // 更新用户信息
        UserModel::update($data);

        return $this->success();

    }

    #[Auth('admin:user:bind_email')]
    public function bindEmail(): \think\response\Json
    {

        $email = $this->request->post('email');
        $code = $this->request->post('code');

        if (Principal::isSuperUser()) {
            return $this->error(['message' => Constants::E_NOT_CHANGE_SUPER_USER]);
        }

        $key = Constants::CACHE_VERIFICATION_CODE_KEY . $email;
        if ($this->verificationCodeService->verifyCode($key, $code)) {
            UserModel::update(['email' => $email], ['id' => Principal::getUserId()]);
            return $this->success();
        } else {
            return $this->error(Constants::E_AUTH_VERIFICATION_CODE_ERROR);
        }

    }

    #[Auth('admin:user:bind_mobile')]
    public function bindMobile(): \think\response\Json
    {

        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');

        if (Principal::isSuperUser()) {
            return $this->error(['message' => Constants::E_NOT_CHANGE_SUPER_USER]);
        }

        $key = Constants::CACHE_VERIFICATION_CODE_KEY . $mobile;
        if ($this->verificationCodeService->verifyCode($key, $code)) {
            UserModel::update(['email' => $mobile], ['id' => Principal::getUserId()]);
            return $this->success();
        } else {
            return $this->error(Constants::E_AUTH_VERIFICATION_CODE_ERROR);
        }

    }

    /**
     * 获得用户配置信息
     *
     * @return \think\response\Json
     */
    public function getProfile(): \think\response\Json
    {
        $result = Principal::getUserInfo();
        return $this->success(['data' => $result]);
    }

    /**
     * 修改用户配置信息
     *
     * @return \think\response\Json
     */
    public function updateProfile(): \think\response\Json
    {

        $fields = "nickname,gender,avatar";
        $data = $this->request->only(explode(',', $fields), 'post');

        if (Principal::isSuperUser()) {
            return $this->error(Constants::E_NOT_CHANGE_SUPER_USER);
        }

        $data['id'] == Principal::getUserId();
        Validate(UserValidate::class)
            ->scene('update')
            ->check($data);
        UserModel::update($data);
        return $this->success();

    }


}